const data0 = JSON.parse(localStorage.getItem("薪资data"));
const data = data0.data;

const aData = JSON.parse(localStorage.getItem("薪资总data"));
const allData = aData.allData;

// 搜索条件的下拉菜单
function List(data) {
    let departmentList = [];
    for (let i = 0; i < data.length; ++i) {
        if (departmentList.indexOf(data[i].department) === -1) {
            departmentList.push(data[i].department);
        }
    }
    for (let i = 0; i < departmentList.length; ++i) {
        $("#departmentList").append(` <option value = ${departmentList[i]}>${departmentList[i]}</option>`);
    }
}

List(data);

$("#abolish").on("click", function () {
    window.location.href ="../薪资/薪资核算表.html"
});



// 创建核算数据
// let accountingData = []
let newData = [];

// 获取新表的信息
let name1;
let month;
let departmentList;
$("#start").on("click", function () {
    $(".ul").removeClass("blue");
    $(".people").addClass("blue");
    $("#ul").addClass("none");
    $("#people").removeClass("none").addClass("block");

    name1 = $("#name").val();
    month = $("#month").val();
    departmentList = $("#departmentList").val();
    for (let i = 0; i < data.length; ++i) {
        if (data[i].department === departmentList) {
            newData.push(data[i]);
        }
    }

    function renderBody(data, currentPage = 1) {
        singlePageNum = 5;
        renderPaging(data, currentPage);
        const statIndex = singlePageNum * (currentPage - 1);
        const endIndex = statIndex + singlePageNum;
        const newData = data.slice(statIndex, endIndex);
        $("#people-information tbody").empty();
        for (let i = 0; i < newData.length; ++i) {
            const checked = data[i].isComplete ? "checked" : "";
            $("tbody").append(`<tr> <td><input type="checkbox" ${checked}  id="${newData[i].id}"> ${newData[i].name}</td><td>${newData[i].id}</td><td>${newData[i].department}</td><td>${newData[i].position}</td><td>${newData[i].type}</td><td>${newData[i].PositiveDates}</td>
            <td>
            <button type="button" class="btn btn-info btn-lg delete" data-toggle="modal" data-target="#myModal"
            id="${newData[i].id}" >删除</button>
            </td>
        </tr>
            `
            );
        }
    }

    function renderPaging(data, currentPage = 1) {
        $("#pages").empty();
        const num = Math.ceil(data.length / singlePageNum);
        for (let i = 1; i <= num; ++i) {
            $("#pages").append(`<span>${i}</span>`);
        }
        $("#pages > span").eq(currentPage - 1).addClass("currentPage");
    }

    // 页码
    $("#pages").on("click", "span", function (event) {
        const currentPage = parseInt($(event.target).text());
        renderBody(newData, currentPage);
    });
    // 上一页
    $("#pre").on("click", function () {
        const currentPage = parseInt($("#pages > .currentPage").text());
        if (currentPage === 1) {
            return;
        }
        else {
            renderBody(newData, currentPage - 1);
        }
    });
    // 下一页
    $("#next").on("click", function () {
        const currentPage = parseInt($("#pages > .currentPage").text());
        if (currentPage === $("#pages span").length) {
            return;
        }
        else {
            renderBody(newData, currentPage + 1);
        }
    });
    let dataId;

    // 删除
    $("tbody").on("click", ".delete", function (event) {
        dataId = parseInt($(event.target).attr("id"));
        for (let i = 0; i < newData.length; ++i) {
            if (dataId === newData[i].id) {
                $(".modal-body").append(`
            <span>姓名：${newData[i].name}，</span>
            <span>id： ${newData[i].id}</span>
        `);
            }
        }
    });
    $("#callOff").on("click", function () {
        $(".modal-body").empty();
    });

    $("#btn-primary").on("click", function () {
        $('#myModal').modal('hide');
        $(".modal-body").empty();
        for (let i = 0; i < newData.length; ++i) {
            if (dataId === newData[i].id) {
                newData.splice(i, 1);
            }
        }
        renderBody(newData, currentPage = 1);
    });

    // 单选
    $("#people-information").on("change", "input", function (event) {
        // 当前选中checkbox
        const dataId1 = Number($(event.target).attr("id"));

        // 当前是否被选中
        const checked = $(event.target).prop("checked");
        // 修改数据
        // 修改该 id 对应data的数据
        for (let i = 0; i < newData.length; ++i) {
            const item = newData[i];
            if (item.id === dataId1) {
                item.isComplete = checked;
            }
        }
        console.log(newData);
        // 重新渲染
        renderBody(newData);
    });

    // 全选
    let a = 1;
    $("#checkAll").on("click", function () {
        if (a % 2 === 1) {
            if ($("#information td input").prop("checked") === false) {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData[i];
                    item.isComplete = true;
                }
            }
            else {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData[i];
                    item.isComplete = true;
                }
            }
        }
        else {
            if ($("#information td input").prop("checked") === false) {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData[i];
                    item.isComplete = false;
                }
            }
            else {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData[i];
                    item.isComplete = false;
                }
            }
        }
        a = a + 1;
        renderBody(newData);
    });
    renderBody(newData);
});



let newData1 = [];
$("#beganAccounting").on("click", function () {
    for (let i = 0; i < newData.length; ++i) {
        if (newData[i].isComplete === true) {
            newData[i].isComplete = false;
            newData1.push(newData[i]);
        }
    }
    $("#ul").removeClass("block").addClass("none");
    $("#people").removeClass("block").addClass("none");
    $("#money").removeClass("none").addClass("block");

    $(".people").removeClass("blue");
    $(".money").addClass("blue");

    function renderBody1(newData1, currentPage = 1) {
        singlePageNum = 5;
        renderPaging1(newData1, currentPage);
        const statIndex = singlePageNum * (currentPage - 1);
        const endIndex = statIndex + singlePageNum;
        newData2 = newData1.slice(statIndex, endIndex);
        $("#money-information tbody").empty();
        for (let i = 0; i < newData2.length; ++i) {
            const checked = newData2[i].isComplete ? "checked" : "";
            $("#money-information tbody").append(`
            <tr> <td><input type="checkbox" ${checked}  id="${newData2[i].id}"> ${newData2[i].name}</td>
            <td>${newData2[i].id}</td>
            <td>${newData2[i].department}</td>
            <td>${newData2[i].position}</td>
            <td>${newData2[i].type}</td>
            <td>${newData2[i].PositiveDates}</td>
            <td>${newData2[i].totalWages}</td>
            <td>${newData2[i].withhold}</td>
            <td>${newData2[i].overtimePay}</td>
            <td>${newData2[i].individualIncomeTax}</td>
            <td>${newData2[i].最终工资}</td>
            <td>
            <button type="button" class="btn btn-info btn-lg delete1" data-toggle="modal" data-target="#myModal"
            id="${newData1[i].id}" >删除</button>
            </td>
        </tr>
            `
            );
        }
    }

    function renderPaging1(newData1, currentPage = 1) {
        $("#money-pages").empty();
        const num = Math.ceil(newData1.length / singlePageNum);
        for (let i = 1; i <= num; ++i) {
            $("#money-pages").append(`<span>${i}</span>`);
        }
        $("#money-pages > span").eq(currentPage - 1).addClass("currentPage");
    }

    renderBody1(newData1);

    // 页码
    $("#money-pages").on("click", "span", function (event) {
        const currentPage = parseInt($(event.target).text());
        renderBody1(newData1, currentPage);
    });
    // 上一页
    $("#pre").on("click", function () {
        const currentPage = parseInt($("#pages > .currentPage").text());
        if (currentPage === 1) {
            return;
        }
        else {
            renderBody1(newData1, currentPage - 1);
        }
    });
    // 下一页
    $("#next").on("click", function () {
        const currentPage = parseInt($("#pages > .currentPage").text());
        if (currentPage === $("#pages span").length) {
            return;
        }
        else {
            renderBody1(newData1, currentPage + 1);
        }
    });

    let dataId1;
    // 删除
    $("tbody").on("click", ".delete1", function (event) {
        dataId1 = parseInt($(event.target).attr("id"));
        for (let i = 0; i < newData1.length; ++i) {
            if (dataId1 === newData1[i].id) {
                $(".modal-body").append(`
            <span>姓名：${newData1[i].name}，</span>
            <span>id： ${newData1[i].id}</span>
        `);
            }
        }
    });
    $("#callOff").on("click", function () {
        $(".modal-body").empty();
    });

    $("#btn-primary").on("click", function () {
        $('#myModal').modal('hide');
        $(".modal-body").empty();
        for (let i = 0; i < newData1.length; ++i) {
            if (dataId1 === newData1[i].id) {
                newData1.splice(i, 1);
            }
        }
        renderBody1(newData1, currentPage = 1);
    });

    a = 1;
    $("#checkAll1").on("click", function () {
        if (a % 2 === 1) {
            if ($("#information td input").prop("checked") === false) {
                for (let i = 0; i < newData1.length; ++i) {
                    const item = newData1[i];
                    item.isComplete = true;
                }
            }
            else {
                for (let i = 0; i < newData1.length; ++i) {
                    const item = newData1[i];
                    item.isComplete = true;
                }
            }
        }
        else {
            if ($("#information td input").prop("checked") === false) {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData1[i];
                    item.isComplete1 = false;
                }
            }
            else {
                for (let i = 0; i < newData.length; ++i) {
                    const item = newData1[i];
                    item.isComplete = false;
                }
            }
        }
        a = a + 1;
        renderBody1(newData1);


        let newData2 = [];
        $("#beganAccounting1").on("click", function () {
            console.log("0");
            for (let i = 0; i < newData1.length; ++i) {
                if (newData1[i].isComplete === true) {
                    newData2.push(newData1[i]);
                }
            }
            let allIndividualIncomeTax = 0;
            let totalWages = 0;
            for (let i = 0; i < newData2.length; ++i) {
                allIndividualIncomeTax += newData2[i].individualIncomeTax;
                console.log(typeof newData2[i].totalWages);
                totalWages += newData2[i].totalWages;
            }
            allData.push(
                {
                    "name": name1,
                    "month": month,
                    "departmentList": departmentList,
                    "state": "进行中",
                    "length": newData2.length,
                    "allIndividualIncomeTax": allIndividualIncomeTax,
                    "totalMoney": totalWages

                }
            );
            localStorage.setItem("薪资总data", JSON.stringify({ allData }));

            window.location.href ="../薪资/薪资核算表.html"
        });

    });































});
